#include "ToolManager.h"
#include "BitmapFont.h"
#include "Player.h"
#include "GamePlayState.h"
#include "Arm.h"


ToolManager::ToolManager(Player* owner)
{
	player = owner;
	magnetactive = false;
	thermoactive = false;
	grappleacitve = false;
	toolsdisabled = false;
	frontArmAvailable = true;
	rearArmAvailable = true;
	frontArm = player->GetFrontArm();
	rearArm = player->GetRearArm();
	fArm = rArm = EMPTY;
}

ToolManager::~ToolManager(void)
{
}

void ToolManager::ToggleTool(ToolType tool)
{
	switch(tool)
	{
	case MAGNET:
		if(tmagnet.GetActive())
		{
			magnetactive = false;
			tmagnet.DeactivateTool();
			if(fArm == MAGNET)
			{
				frontArmAvailable = true;
				frontArm->SetTool( EMPTY, nullptr );
				fArm = EMPTY;
			}
			else if( rArm == MAGNET )
			{
				rearArmAvailable = true;
				rearArm->SetTool( EMPTY, nullptr );
				rArm = EMPTY;
			}
		}
		else
		{
			if(rearArmAvailable)
			{
				magnetactive = true;
				tmagnet.ActivateTool(rearArm);
				rearArmAvailable = false;
				rArm = MAGNET;
				rearArm->SetTool( MAGNET, &tmagnet );
			}
			else if(frontArmAvailable)
			{
				magnetactive = true;
				tmagnet.ActivateTool(frontArm);
				frontArmAvailable = false;
				fArm = MAGNET;
				frontArm->SetTool( MAGNET, &tmagnet );
			}
		}
		break;
	case GRAPPLE:
		if(tgrapple.GetActive())
		{
			grappleacitve = false;
			tgrapple.DeactivateTool();
			if(fArm == GRAPPLE)
			{
				frontArmAvailable = true;
				frontArm->SetTool( EMPTY, nullptr );
				frontArm->SetFrozen( false );
				fArm = EMPTY;
			}
			else if( rArm == GRAPPLE )
			{
				rearArmAvailable = true;
				rearArm->SetTool( EMPTY, nullptr );
				rearArm->SetFrozen( false );
				rArm = EMPTY;
			}
		}
		else
		{
			if(rearArmAvailable)
			{
				grappleacitve = true;
				tgrapple.ActivateTool(rearArm);
				rearArmAvailable = false;
				rArm = GRAPPLE;
				rearArm->SetTool( GRAPPLE, &tgrapple );
				rearArm->SetFrozen( true );
			}
			else if(frontArmAvailable)
			{
				grappleacitve = true;
				tgrapple.ActivateTool(frontArm);
				frontArmAvailable = false;
				fArm = GRAPPLE;
				frontArm->SetTool( GRAPPLE, &tgrapple );
				frontArm->SetFrozen( true );
			}
		}
		break;
	case THERMO:
		if(tthermo.GetActive())
		{
			thermoactive = false;
			tthermo.DeactivateTool();
			if(fArm == THERMO)
			{
				frontArmAvailable = true;
				frontArm->SetTool( EMPTY, nullptr );
				fArm = EMPTY;
			}
			else if( rArm == THERMO )
			{
				rearArmAvailable = true;
				rearArm->SetTool( EMPTY, nullptr );
				rArm = EMPTY;
			}
		}
		else
		{
			if(rearArmAvailable)
			{
				thermoactive = true;
				tthermo.ActivateTool(rearArm);
				rearArmAvailable = false;
				rArm = THERMO;
				rearArm->SetTool( THERMO, &tthermo );
			}
			else if(frontArmAvailable)
			{
				thermoactive = true;
				tthermo.ActivateTool(frontArm);
				frontArmAvailable = false;
				fArm = THERMO;
				frontArm->SetTool( THERMO, &tthermo );
			}
		}
		break;
	}
}

void ToolManager::Update(float deltaTime)
{
	tmagnet.Update(deltaTime);
	tgrapple.Update(deltaTime);
	tthermo.Update(deltaTime);
}

void ToolManager::RenderFront( void ) const
{
	if (fArm == MAGNET)
	{
		tmagnet.Render();
	}
	else if (fArm == GRAPPLE)
	{
		tgrapple.Render();
	}
	else if (fArm == THERMO)
	{
		tthermo.Render();
	} 
}

void ToolManager::RenderRear( void ) const
{
	if (rArm == MAGNET)
	{
		tmagnet.Render();
	}
	else if (rArm == GRAPPLE)
	{
		tgrapple.Render();
	}
	else if (rArm == THERMO)
	{
		tthermo.Render();
	} 
}

void ToolManager::Disable(void)
{
	toolsdisabled = true;
	tgrapple.Disable();
	tthermo.Disable();

	if(rArm == GRAPPLE)
	{
		rearArmAvailable = true;
		rearArm->SetTool( EMPTY, nullptr );
		rearArm->SetFrozen( false );
		rArm = EMPTY;
		tgrapple.DeactivateTool();
	}
	else if(fArm == GRAPPLE)
	{
		frontArmAvailable = true;
		frontArm->SetTool( EMPTY, nullptr );
		frontArm->SetFrozen( false );
		fArm = EMPTY;
		tgrapple.DeactivateTool();
	}

}

void ToolManager::Enable(void)
{
	toolsdisabled = false;
	tgrapple.Enable();
	tthermo.Enable();
}

void ToolManager::SetToolHeading(ToolType tool, tVector2D heading)
{
	switch(tool)
	{
	case GRAPPLE:
		tgrapple.SetHeading(heading);
		break;
	case MAGNET:
		tmagnet.SetHeading(heading);
		break;
	case THERMO:
		tthermo.SetHeading(heading);
		break;
	}
}
